<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="hAdd">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <!-- <el-table-column align="center" width="200" label="角色" prop="name"> -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else> {{ row.name }} </span>
          </template>
        </el-table-column>

        <el-table-column align="center" width="400" label="启用" prop="state">
          <template v-slot="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>
              {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <!-- <el-table-column align="center" label="描述" prop="description"> -->
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else> {{ row.description }} </span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="hOK(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="getEmployeeByid(row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="hEdit(row)">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button
                  slot="reference"
                  style="margin-left:10px"
                  size="mini"
                  type="text"
                >
                  删除
                </el-button>
              </el-popconfirm>
            </template></template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          layout="sizes,prev, pager, next"
          :background="true"
          :total="total"
          :page-size="pagesize"
          :page-sizes="[ 4, 5 , 6, 10]"
          :current-page="page"
          @current-change="currentChange"
          @size-change="handleSizeChange"
        />

      </el-row>
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="roleForm" label-width="120px" :rules="rules" :model="roleForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="roleForm.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="doAdd">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <assignpermission
      ref="assign"
      :show-dialogperm.sync="showDialogperm"
      :role-id="roleId"
    />
  </div>
</template>
<script>
import { addRole, getRoleList, updateRole, delRole } from '@/api/role'
import assignpermission from './components/assignpermission.vue'
export default {
  name: 'Role',
  components: { assignpermission },
  data() {
    return {
      list: [],
      // pageParams: {
      page: 1, // 第几页
      pagesize: 5, // 每页多少条
      total: 0,

      // }
      showDialog: false,
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showDialogperm: false,
      roleId: -1
    }
  },
  created() {
    this.loadRoleList()
  },

  methods: {
    getEmployeeByid(id) {
      this.roleId = id
      this.showDialogperm = true
      // this.$refs.
      this.$nextTick(() => {
        this.$refs.assign.getPermissionByid()
      })
    },
    async hDel(id) {
      console.log(id)
      await delRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) this.page--
      }
      this.loadRoles()
    },
    async confirmDel(id) {
      await delRole(id) // 后端删除
      this.$message.success('删除角色成功')
      // 删除的如果是最后一个
      if (this.list.length === 1) this.pageParams.page--
      this.loadRoleList()
    },
    async hOK(row) {
      // console.log(row)
      // if (!row.editRow.name || !row.rditRow.description) {
      //   this.$message.error('请输入内容')
      //   return
      // }
      // await updateRole(row.editRow)
      // this.$message.success('更新成功')
      // // eslint-disable-next-line require-atomic-updates
      // row.isEdit = false
      // this.loadRoleList()
      if (row.editRow.name && row.editRow.description) {
        // 下一步操作
        await updateRole({ ...row.editRow, id: row.id })
        // 更新成功
        this.$message.success('更新角色成功')
        // 更新显示数据  退出编辑状态
        // row.name = row.editRow.name // eslint的一校验 误判
        // Object.assign(target, source)
        Object.assign(row, {
          ...row.editRow,
          isEdit: false // 退出编辑模式
        }) // 规避eslint的误判
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    hEdit(row) {
      // console.log(row)
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    doAdd() {
      this.$refs.roleForm.validate(async vaild => {
        if (!vaild) return
        const res = await addRole(this.roleForm)
        console.log(res)
        this.$message.success('添加角色成功')
        // this.loadRoleList()
        this.total = this.total + 1
        this.page = parseInt(this.total / this.pagesize) + 1
        console.log(parseInt(this.total / this.pagesize) + 1)
        this.loadRoleList()
        this.close()
      })
    },
    close() {
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    hAdd() {
      this.showDialog = true
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadRoleList()
    },

    currentChange(page) {
      this.page = page
      this.loadRoleList()
    },
    async loadRoleList() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      console.log(res)
      this.list = res.data.rows
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
        // item.isEdit = true
      })
      this.total = res.data.total // 赋值数据
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
